from rest_framework import serializers
from django.contrib.auth.hashers import make_password

from users.models import User


class AdminManageSerializer(serializers.ModelSerializer):
    mobile = serializers.CharField(max_length=11,label="手机号",read_only=True)

    class Meta:
        model = User
        fields = ("id","username","email","phone_number","mobile","password","groups","user_permissions")
        extra_kwargs = {
            "phone_number":{"write_only":True},
            "password":{"write_only":True},
            # "groups":{"write_only":True},
            # "user_permissions":{"write_only":True},

        }

    def validate(self, attrs):
        attrs["is_staff"] = True
        attrs["password"] = make_password(attrs["password"])

        return attrs
# {
#     "id": "用户id",
#     "username": "用户名",
#     "email": "邮箱",
#     "mobile": "手机号"
# }


 # [
 #        {
 #            "id": 1,
 #            "name": "广告组"
 #        },
 #        {
 #            "id": 2,
 #            "name": "商品SKU组"
 #        },
 #        ......
 #    ]